package com.apartment.own.controller;

import java.util.List;

import com.apartment.own.request.res.ResArticleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.apartment.common.annotation.Log;
import com.apartment.common.core.controller.BaseController;
import com.apartment.common.core.domain.AjaxResult;
import com.apartment.common.enums.BusinessType;
import com.apartment.own.domain.OwnResArticle;
import com.apartment.own.service.IOwnResArticleService;
import com.apartment.common.utils.poi.ExcelUtil;
import com.apartment.common.core.page.TableDataInfo;

/**
 * 物品Controller
 *
 * @author @Dog_E
 * @date 2020-08-28
 */
@RestController
@RequestMapping("/own/article")
@Api(value = "物品", tags = "物品")
public class OwnResArticleController extends BaseController {
    @Autowired
    private IOwnResArticleService ownResArticleService;

    /**
     * 查询物品列表
     */
    @PreAuthorize("@ss.hasPermi('own:article:list')")
    @GetMapping("/list")
    @ApiOperation("3级查询物品")
    @ApiImplicitParam(name = "level",value = "3",required = true)
    public TableDataInfo list(OwnResArticle ownResArticle) {
        startPage();
        List<OwnResArticle> list = ownResArticleService.selectOwnResArticleList(ownResArticle);
        return getDataTable(list);
    }

    /**
     * 导出物品列表
     */
    @PreAuthorize("@ss.hasPermi('own:article:export')")
    @Log(title = "物品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnResArticle ownResArticle) {
        List<OwnResArticle> list = ownResArticleService.selectOwnResArticleList(ownResArticle);
        ExcelUtil<OwnResArticle> util = new ExcelUtil<OwnResArticle>(OwnResArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 获取物品详细信息
     */
    @PreAuthorize("@ss.hasPermi('own:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ownResArticleService.selectOwnResArticleById(id));
    }

//    /**
//     * 新增物品
//     */
//    @PreAuthorize("@ss.hasPermi('own:article:add')")
//    @Log(title = "物品", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody OwnResArticle ownResArticle)
//    {
//        return toAjax(ownResArticleService.insertOwnResArticle(ownResArticle));
//    } 


    /**
     * 修改物品
     */
    @PreAuthorize("@ss.hasPermi('own:article:edit')")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改物品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", dataType = "String", value = "id", required = true),
            @ApiImplicitParam(name = "parentId", dataType = "String", value = "父id 没有上一级则是 -1"),
            @ApiImplicitParam(name = "itemsName", dataType = "String", value = "物品名称", required = true),
    })
    public AjaxResult edit(@RequestBody OwnResArticle ownResArticle) {
        return toAjax(ownResArticleService.updateOwnResArticle(ownResArticle));
    }

    /**
     * 删除物品
     */
    @PreAuthorize("@ss.hasPermi('own:article:remove')")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除物品")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return ownResArticleService.deleteOwnResArticleByIds(ids);
    }

    /**
     * @param resArticleDTO: 物品对象DTO
     * @Description: 新增物品
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/9/3 0003
     **/
    @PreAuthorize("@ss.hasPermi('own:article:add')")
    @Log(title = "物品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加物品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", dataType = "String", value = "父id 没有上一级则是 -1"),
            @ApiImplicitParam(name = "itemsName", dataType = "String", value = "物品名称", required = true),
    })
    public AjaxResult add(@RequestBody ResArticleDTO resArticleDTO) {
        return toAjax(ownResArticleService.addOwnResArticle(resArticleDTO));
    }

    /**
     * @param : 物品级联查询
     * @Description: 级联查询
     * @Author: @Dog_E
     * @Date: 2020/9/3 0003
     **/
    @PreAuthorize("@ss.hasPermi('own:article:list')")
    @GetMapping("/cascadeQueryList")
    @ApiOperation("物品级联查询")
    public AjaxResult cascadeQuery(ResArticleDTO resArticleDTO) {
        List<ResArticleDTO> list = ownResArticleService.cascadeQueryList(resArticleDTO);
        return AjaxResult.success(list);
    }


}
